function func(data) {
  let s = ''
  data.forEach(e => {
    s += `<tr>
      <td>${e.jobId}</td>
      <td>${e.jobName}</td>
      <td>${e.dept.deptName}</td>
      <td>${e.jobState === '1' ? '正常' : '暂无职位'}</td>
      <td>
        <a href="javascript:void(0);" onclick="update('${e.jobId}','${e.jobName}','${e.jobDeptId}')" class="tablelink">修改</a>
        <a href="javascript:void(0);" onclick="grant(${e.jobId})" class="tablelink">赋权</a>
        <a href="javascript:void(0);" class="tablelink" onclick="zx(${e.jobId},${e.jobState})">${e.jobState === '1' ? '禁用' : '恢复'}</a>
      </td>
    </tr>`
  });
  $('#tbody').html(s)
  $('.tablelist tbody tr:odd').addClass('odd');
}

function getData() {
  axiosRequest1("get", "/job/joblist").then(res => {
    const { code, message, data } = res.data
    if (code === 200) {
      func(data)
    } else {
      showMessage(message, 2000)
    }
  }, err => {
    console.log(err);
  })
}

function getDept() {
  axiosRequest1("get", "/dept/deptList").then(res => {
    const {
      code,
      data,
      message
    } = res.data
    if (code === 200) {
      let str = '<option value=0>请选择部门</option>'
      data.forEach(item => {
        str += `<option value='${item.deptId}'>${item.deptName}</option>`
      })
      $('#deptSelect').html(str)
    } else {
      showMessage(message, 2000)
    }
  }, error => {
    console.log(error);
  })
}

function search() {
  let jobName = $('#jobName').val().trim()
  let deptId = $('#deptSelect').val().trim()
  let obj = { jobName, deptId }
  axiosRequest1('post', '/job/queryCondition', obj).then(res => {
    const { code, message, data } = res.data
    if (code === 200) {
      func(data)
    } else {
      showMessage(message, 2000)
    }
  }, err => {
    console.log(err);
  })
}

function zx(id,state){
  if(state==1){
    tipOpen('是否确认注销此职位？')
  }else{
    tipOpen('是否确认恢复此职位？')
  }
  timer=setInterval(() => {
    if(flag){
      tipClose()
      flag=false;
      axiosRequest1('get',`/job/zx/${id}/${state}`).then(res=>{
        const {code,message} = res.data
        showMessage(res.data.message,2000)
        if(code===200){
          search()
        }
      })
    }
  }, 10);
}

function update(jid,jobName,deptId){
  let uri=encodeURI(`jid=${jid}&jobName=${jobName}&deptSelect=${deptId}`)
  location.href=`positionUpdate.html?${uri}`
}

function grant(id){
  location.href=`positionGrant.html?id=${id}`
}